<?php
//$arr = array("2", "3", "4", "7", "8", "10", "22", "23"); //Job list
$arr = array("29", "35", "30", "31", "40", "36", "41", "32", "33", "39", "34"); //Craft list

$type = "craft";

for ($i=0 ; $i<=count($arr); $i++){
	$class = $arr[$i];
	//$class = "23&page=2";
	$stringToWrite = "<?xml version=\"1.0\" encoding=\"utf-8\"?><ffxivcalc><abilities>\n";
	
	$url = "http://ffxiv.zam.com/en/abilitylist.html?class=".$class;// . $arr[$i];
	$input = file_get_contents($url) or die("Could not access file: ".$url);
	
	$tableStart = strrpos ($input, "<table class=\"datatable sortable\"");
	//echo "Start: ".$tableStart."<br>";
	$endTable = strrpos ($input, "</table>", $tableStart);
	//echo "End: ".$endTable."<br>";
	$input = substr($input,$tableStart,($endTable-$tableStart));
	//echo $input;
	
	$regexp = "<a\s[^>]*href=(\"??)([^\" >]*?)\\1[^>]*>(.*)<\/a>";
	  if(preg_match_all("/".$regexp."/siU", $input, $matches, PREG_SET_ORDER)) {
		foreach($matches as $match) {
			$stringToWrite .= "<skill id=\"".$class."\" type=\"".$type."\">";
			//echo $match[2];
			
			// Get details of ability from yg with the id from zam
			$abilityUrl = "http://ffxiv.zam.com".$match[2];
			$abilityInput = file_get_contents($abilityUrl);
			
			$abilityDescription = findString("<div class=\"xivtt xivtt-abil\">", NULL, "<div class=\"xivtt-inner\">" ,"<div class=\"tabwrapper\">", $abilityInput);
			
			$head = getPart("<div class=\"xivtt-head\">", $abilityDescription);
			
			//echo $head;
			
			$name = findString(NULL, NULL, "<h2>", "</h2>", $head);
			$stringToWrite .= "<name>".$name."</name>";
			
			$cost = findString(NULL, NULL, "</span>", "</div>", $head);
		
			$stringToWrite .= "<actionCost>".getNumber($cost)."</actionCost>";
			
			$body =  getPart("<div class=\"xivtt-body\">", $abilityDescription);
			
			$rank = findString("Optimal Rank", NULL, "</span>", "<br>", $body);
			
			$stringToWrite .= "<rank>".getNumber($rank)."</rank>";
			
			$mp = findString("MP Cost", NULL, "</span>", "<br>", $body);
			
			$stringToWrite .= "<mp>".getNumber($mp)."</mp>";
			
			$tp = findString("TP Cost", NULL, "</span>", "<br>", $body);
			
			$stringToWrite .= "<tp>".getNumber($tp)."</tp>";
			
			$text = findString(NULL, NULL, "<div style=\"margin:5px 0 0\">", "</div>", $body);
			
			$stringToWrite .= "<text>".$text."</text>";
			
			//For each ability get the icon from yg.com
			$skillUrl = "http://ffxiv.yg.com/api/lookup?s=".urlencode($name)."&o=skill";
			$skillJSON = file_get_contents($skillUrl);
			$icon = findString($name, NULL, "\"medium\":\"", "\",\"", $skillJSON);
			$stringToWrite .= "<mediumIcon>".str_replace("\\","",$icon)."</mediumIcon>";  
			$icon = findString($name, NULL, "\"large\":\"", "\"}", $skillJSON);
			$stringToWrite .= "<largeIcon>".str_replace("\\","",$icon)."</largeIcon>";  
		  
		  $stringToWrite .= "</skill>\n";
		}
	  }
	 
	$stringToWrite .= "</abilities></ffxivcalc>";
	echo $stringToWrite;
	
	
	// Write abilities to their file
	$myFile = "../test/dataImport/".$class.".xml";
	$fh = fopen($myFile, 'w');
	
	
	fwrite($fh, $stringToWrite);
	fclose($fh);
}

function getPart($anchor, $string){

	$start = strpos ($string, $anchor);
	$firstEnder = strpos ($string, "</div>" , $start);
	$secondEnder = strpos ($string, "</div>" , $firstEnder+6);
	Return substr ($string, $start+strlen($anchor), $secondEnder-($start+strlen($anchor))); 

}
function getNumber($text){
	if ($text != ""){
		if(preg_match("/(\d+)/", $text, $match)){
			return $match[0];
		}else{
			return "";
		}
	}else{
		return "";
	}
}

function findString($parentAnchor, $childAnchor, $startT, $endT, $file){
		$abort = false;
		$varDefStart = 1;
		if (!is_null($parentAnchor)){
			$varDefStart = strpos ($file, $parentAnchor);
			if ($varDefStart== False){
				$abort = true;
			}
		}
		if (!is_null($childAnchor)){
			$varDefStart = strpos ($file, $childAnchor, $varDefStart);
			if ($varDefStart== False){
				$abort = true;
			}
		}
		//if ($varDefStart >= 0 && $varDefStart<=strlen($file) && !($varDefStart==False)) {
			//echo "first anchor:".$varDefStart."<br>";
		$startPointRaw  = strpos  ($file, $startT, $varDefStart);
		if (($startPointRaw!= False) && ($abort == false)){
			$startPoint = $startPointRaw+strlen($startT);
			//echo "startpoint:".$startPoint."<br>";
			$endPoint = strpos  ($file, $endT, $startPoint);
			//echo "endpoint:".$endPoint."<br>";
			Return substr ($file, $startPoint, ($endPoint-($startPoint)));
		}else{
			Return "";
		}

}

?>